<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import CustomNvabar from './components/CustomNavbar.vue'
import CategoryPanal from './components/CategoryPanal.vue'
import HotPanal from './components/HotPanal.vue'
import PageSkeleLon from './components/PageSkelelon.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { reactive, ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem, PageParams } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'
import { useGuessList } from '@/composables/index'

const { guessRef, onScrolltolower } = useGuessList()

const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getBannerData(), getPanalData(), getHotData()])
  isLoading.value = false
})
//banner
const bannerList = ref<BannerItem[]>([])
const getBannerData = async () => {
  const res = await getHomeBannerAPI(1)
  bannerList.value = res.result
}

//分类
const panelData = ref<CategoryItem[]>([])
const getPanalData = async () => {
  const res = await getHomeCategoryAPI()
  panelData.value = res.result
}

// 热门推荐
const hotData = ref<HotItem[]>([])
const getHotData = async () => {
  const res = await getHomeHotAPI()
  hotData.value = res.result
}

// 下拉刷新状态
const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData() // 加载数据
  await Promise.all([getBannerData(), getPanalData(), getHotData(), guessRef.value?.getGuessData()]) // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <CustomNvabar />
  <scroll-view
    class="scroll-view"
    scroll-y
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="onScrolltolower"
  >
    <PageSkeleLon v-if="isLoading" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanal :list="panelData" />
      <HotPanal :list="hotData" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f8f8f8;
}
.scroll-view {
  flex: 1;
}
</style>
